class MyMetaClass(type):
    def __new__(meta, name, bases, attrs):
        '''
        在 Python 中，__new__ 是一个用于创建新对象的特殊方法。当你自定义一个元类时，__new__ 方法会在类对象被创建时调用，而不是实例对象的创建。

        :param meta: 这里 meta 是元类 MyMetaClass
        :param name: 这是新类的名称。对于 Myclass，它的值是 'Myclass'
        :param bases: 这是新类的基类。对于 Myclass，它是 (), 因为没有显式指定基类，默认是继承自 object
        :param attrs: 这是新类的属性字典，包含新类的所有变量和方法
        '''
        print(meta, "__new__ is called")
        # 动态添加属性
        attrs['name'] = "zhangsan"
        attrs['talk'] = lambda self: print("hello")
        return super(MyMetaClass, meta).__new__(meta, name, bases, attrs)

    @classmethod
    def __prepare__(metacls, name, bases, **kwargs):
        print(metacls, "__prepare__ is called")
        return super().__prepare__(name, bases, **kwargs)

    def __init__(cls, name, bases, attrs, **kwargs):
        print(cls, "__init__ is called")
        super().__init__(name, bases, attrs)

    def __call__(cls, *args, **kwargs):
        print(cls, "__call__ is called")
        return super().__call__(*args, **kwargs)


# 此处会调用MyMetaClass.__prepare__和MyMetaClass.__new__
class Myclass(metaclass=MyMetaClass):
    age = 20

    def __init__(self):
        super().__init__()
        self.base = 'test'

    def print(self):
        print(self.base)


if __name__ == '__main__':
    cla = Myclass()
    print(cla.name)
    cla.talk()
    print(cla.__dir__())

'''
<class '__main__.MyMetaClass'> __prepare__ is called
<class '__main__.MyMetaClass'> __new__ is called
<class '__main__.Myclass'> __init__ is called
<class '__main__.Myclass'> __call__ is called
zhangsan
hello
['base', '__module__', 'age', '__init__', 'print', 'name', 'talk', '__dict__', '__weakref__', '__doc__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
'''
